<?php


require_once 'vendor/autoload.php';
//
//
//class Ac extends \Fw\db\ActiveRecord
//{
//
//
//	public static function tableName()
//	{
//		return 'aaaa';
//	}
//
//}
//
////
//$n = Ac::find()->like('ok', 'aa')->where('a', '>', 'c')->first();


//$keys = array("a", "b", "c", "d");
//$a1 = array_fill_keys($keys, ['blue']);
//print_r($a1);

class it implements \Iterator
{

	public $models;
	public $index = 0;

	/**
	 * Return the current element
	 * @link https://php.net/manual/en/iterator.current.php
	 * @return mixed Can return any type.
	 * @since 5.0.0
	 */
	public function current()
	{
		// TODO: Implement current() method.
		return $this->models[$this->index];
	}

	/**
	 * Move forward to next element
	 * @link https://php.net/manual/en/iterator.next.php
	 * @return void Any returned value is ignored.
	 * @since 5.0.0
	 */
	public function next()
	{
		$this->index++;
		$value = $this->models[$this->index];
		$value->b = 0;

		// TODO: Implement next() method.
		$this->models[$this->index] = $value;
	}

	/**
	 * Return the key of the current element
	 * @link https://php.net/manual/en/iterator.key.php
	 * @return mixed scalar on success, or null on failure.
	 * @since 5.0.0
	 */
	public function key()
	{
		// TODO: Implement key() method.
		return $this->index;
	}

	/**
	 * Checks if current position is valid
	 * @link https://php.net/manual/en/iterator.valid.php
	 * @return boolean The return value will be casted to boolean and then evaluated.
	 * Returns true on success or false on failure.
	 * @since 5.0.0
	 */
	public function valid()
	{
		// TODO: Implement valid() method.
		return isset($this->models[$this->index]);
	}

	/**
	 * Rewind the Iterator to the first element
	 * @link https://php.net/manual/en/iterator.rewind.php
	 * @return void Any returned value is ignored.
	 * @since 5.0.0
	 */
	public function rewind()
	{
		$this->index = 0;
	}
}

//$abb = [];
//for ($b = 0; $b < 10; $b++) {
//	$a = new stdClass();
//	$a->b = $b;
//	$abb[] = $a;
//}
//
//$n = new it();
//$n->models = $abb;
//foreach ($n as $value) {
//	var_dump($value);
//}
//

//$grpc = \Fw\http\Grpc::getInstance();
//$grpc->setHost('127.0.0.1');
//$grpc->setPort('33069');
//$result = $grpc->request('', []);
//
//router()->gRpc('get/user/list', 'GrpcController@log', 33007, '127.0.0.1');
//
//
//var_dump(serialize('[{"week":50,"start_date":"2019-09-16","end_date":"2019-09-23"},{"week":49,"start_date":"2019-09-09","end_date":"2019-09-16"},{"week":48,"start_date":"2019-09-02","end_date":"2019-09-09"},{"week":47,"start_date":"2019-08-26","end_date":"2019-09-02"},{"week":46,"start_date":"2019-08-19","end_date":"2019-08-26"},{"week":45,"start_date":"2019-08-12","end_date":"2019-08-19"},{"week":44,"start_date":"2019-08-05","end_date":"2019-08-12"},{"week":43,"start_date":"2019-07-29","end_date":"2019-08-05"},{"week":42,"start_date":"2019-07-22","end_date":"2019-07-29"},{"week":41,"start_date":"2019-07-15","end_date":"2019-07-22"},{"week":40,"start_date":"2019-07-08","end_date":"2019-07-15"},{"week":39,"start_date":"2019-07-01","end_date":"2019-07-08"},{"week":38,"start_date":"2019-06-24","end_date":"2019-06-30"},{"week":37,"start_date":"2019-06-17","end_date":"2019-06-24"},{"week":36,"start_date":"2019-06-10","end_date":"2019-06-17"},{"week":35,"start_date":"2019-06-03","end_date":"2019-06-10"},{"week":34,"start_date":"2019-05-27","end_date":"2019-06-03"},{"week":33,"start_date":"2019-05-20","end_date":"2019-05-27"},{"week":32,"start_date":"2019-05-13","end_date":"2019-05-20"},{"week":31,"start_date":"2019-05-06","end_date":"2019-05-13"},{"week":30,"start_date":"2019-04-29","end_date":"2019-05-06"},{"week":29,"start_date":"2019-04-22","end_date":"2019-04-29"},{"week":28,"start_date":"2019-04-15","end_date":"2019-04-22"},{"week":27,"start_date":"2019-04-08","end_date":"2019-04-15"},{"week":26,"start_date":"2019-04-01","end_date":"2019-04-08"},{"week":25,"start_date":"2019-03-25","end_date":"2019-04-01"},{"week":24,"start_date":"2019-03-18","end_date":"2019-03-25"},{"week":23,"start_date":"2019-03-11","end_date":"2019-03-18"},{"week":22,"start_date":"2019-03-04","end_date":"2019-03-11"},{"week":21,"start_date":"2019-02-25","end_date":"2019-03-04"},{"week":20,"start_date":"2019-02-18","end_date":"2019-02-25"},{"week":19,"start_date":"2019-02-11","end_date":"2019-02-18"},{"week":18,"start_date":"2019-02-04","end_date":"2019-02-11"},{"week":17,"start_date":"2019-01-28","end_date":"2019-02-04"},{"week":16,"start_date":"2019-01-21","end_date":"2019-01-28"},{"week":15,"start_date":"2019-01-14","end_date":"2019-01-20"},{"week":14,"start_date":"2019-01-07","end_date":"2019-01-13"},{"week":13,"start_date":"2018-12-31","end_date":"2019-01-06"},{"week":12,"start_date":"2018-12-24","end_date":"2018-12-30"},{"week":11,"start_date":"2018-12-17","end_date":"2018-12-23"},{"week":10,"start_date":"2018-12-10","end_date":"2018-12-16"},{"week":9,"start_date":"2018-12-03","end_date":"2018-12-09"},{"week":8,"start_date":"2018-11-26","end_date":"2018-12-02"},{"week":7,"start_date":"2018-11-19","end_date":"2018-11-25"},{"week":6,"start_date":"2018-11-12","end_date":"2018-11-18"},{"week":5,"start_date":"2018-11-05","end_date":"2018-11-11"},{"week":4,"start_date":"2018-10-29","end_date":"2018-11-04"},{"week":3,"start_date":"2018-10-22","end_date":"2018-10-28"},{"week":2,"start_date":"2018-10-15","end_date":"2018-10-21"},{"week":1,"start_date":"2018-10-08","end_date":"2018-10-14"}]'));

//
//$array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
//$array = array_reverse($array);
//foreach ($array as $value) {
//	echo $value . '_' . next($array) . PHP_EOL;
//}

set_error_handler(function () {
	return func_get_args();
});

set_exception_handler(function () {
	return func_get_args();
});

interface Handler
{

//	public function handler($next, $param);

}

class Pipeline
{

	private $execs = [];

	/** @var array $end */
	private $end;

	/**
	 * @param Handler $exec
	 * @return $this
	 */
	public function then(Handler $exec)
	{
		$this->execs[] = $exec;
		return $this;
	}


	/**
	 * @param $handler
	 * @param $params
	 * @return mixed
	 * @throws
	 */
	public function run(\Closure $handler, array $params = [])
	{
		$result = $this->getFirst()($params);
		if (!$result) {
			return $this->runEndHandler();
		}
		foreach ($this->execs as $exec) {
			$result = $this->getNext($handler)($result);
			if (!$result) {
				return $this->runEndHandler();
			}
		}
		return $this->runEndHandler();
	}

	/**
	 * @param $data
	 * @return mixed
	 */
	public function handler(...$data)
	{
//		go(function ($endHandler, $result) {
//			echo '执行结果';
//			call_user_func($endHandler, $result);
//		}, [$this->end, $data]);

		var_dump($data);
		return $data;
	}

	/**
	 * @param $default
	 * @return mixed
	 */
	public function getNext($default)
	{
		$callback = next($this->execs);
		if (empty($callback)) {
			return $default;
		}
		return [$callback, 'handler'];
	}

	public function getFirst()
	{
		return [current($this->execs), 'handler'];
	}

	private function runEndHandler()
	{
		return call_user_func($this->end);
	}

	/**
	 * @param Handler $handler
	 * @return $this
	 */
	public function end(Handler $handler)
	{
		$this->end = [$handler, 'handler'];
		return $this;
	}

	/**
	 * @return $this
	 */
	public function catch()
	{
		set_error_handler([$this, 'error']);
		set_exception_handler([$this, 'handler']);
		return $this;
	}

	public function error()
	{
		var_dump(func_get_args());
	}

	public function shutdown()
	{
	}

}

class Test implements Handler
{

	private $o;

	public function __construct($o)
	{
		$this->o = $o;
	}

	public function handler($param)
	{
		var_dump($this->o);
		return true;
	}

}

class ETest implements Handler
{

	private $o;

	public function __construct($o)
	{
		$this->o = $o;
	}

	public function handler($next, $param)
	{
//		throw new Exception('WO BAO CUO LO');
		return $next($param);
	}

}

class End implements Handler
{

	private $o;

	public function __construct($o)
	{
		$this->o = $o;
	}

	public function handler()
	{
		$param = func_get_args();
		var_dump($param);
		return $param;
	}

}

class ErrorHandler implements Handler
{

	public function handler()
	{
		return (new class
		{
			public function handler()
			{
				echo 'error';
			}
		});
	}

}


class Collect implements IteratorAggregate
{

	public $_items = [];

	public function getIterator()
	{
		return new ModelCollect($this->_items);
	}

}

class ModelCollect extends ArrayIterator
{

	public $index = 0;

	public $array;

	public function __construct($array = array(), $flags = 0)
	{
		$this->array = $array;
		parent::__construct($array, $flags);
	}


	public function next()
	{
		$modelParams = $this->array[$this->index];
		$this->array[$this->index] = \Fw\db\ActiveRecord::propute($modelParams);
		$this->index++;
	}


}


$node = [
	'id'          => 20,
	'name'        => '5',
	'probability' => 0.4,
	'node'        => [
		'id'          => 16,
		'name'        => '10',
		'probability' => 0.1,
		'node'        => [
			'id'          => 19,
			'name'        => '20',
			'probability' => 0.025,
			'node'        => [
				'id'          => 15,
				'name'        => '50',
				'probability' => 0.01,
				'node'        => [
					'id'          => 17,
					'name'        => '100',
					'probability' => 0.001,
					'node'        => [
						'id'          => 13,
						'name'        => '500',
						'probability' => 0.0001,
						'node'        => []
					]
				]
			]
		]
	]
];

function luck($node, $parent)
{
	$rand = mt_rand(1, 10000);
	if (empty($node['node'])) {
		return [null, $parent];
	}
	echo $node['name'] . ':' . $rand . ':' . ($node['probability'] * 10000) . PHP_EOL;
	if ($rand < $node['probability'] * 10000) {
		return [$node['node'], $node];
	} else {
		return [$node['node'], $parent];
	}
}

//
//function reward($node)
//{
//	$parent = null;
//	while (true) {
//		[$node, $parent] = luck($node, $parent);
//		if (empty($node)) {
//			break;
//		}
//	}
//	return $parent;
//}
//
//$_tmp = [];
//for ($i = 0; $i < 100000; $i++) {
//	$reward = reward($node);
//	if (empty($reward)) {
//		if (!isset($_tmp['谢谢惠顾'])) {
//			$_tmp['谢谢惠顾'] = 0;
//		}
//		$_tmp['谢谢惠顾'] += 1;
//	} else {
//		if (!isset($_tmp[$reward['name'] . '星币'])) {
//			$_tmp[$reward['name'] . '星币'] = 0;
//		}
//		$_tmp[$reward['name'] . '星币'] += 1;
//	}
//}
//asort($_tmp, SORT_DESC);
//print_r($_tmp);

$apiKey = '1.0 user/detail null {"id":1000}';

$a = 123;
$b = 456;
$c = 789;
$str = pack('h',$apiKey);
var_dump($str);
var_dump(unpack('h',$str));

